package com.bdqn.controller;

import com.bdqn.LayUI.Layui;
import com.bdqn.Service.Contract.ContractService;
import com.bdqn.Service.IdMapping.IdMappingService;
import com.bdqn.Service.Orders.OrdersService;
import com.bdqn.Service.Supplier.SupplierService;
import com.bdqn.entity.*;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 合同控制器,刘嘉辉负责
 */
@Controller
@ResponseBody
@RequestMapping("/Cont")
public class ContractController {
    @Autowired
    ContractService contractService;
    @Autowired
    OrdersService ordersService;
    @Autowired
    IdMappingService idMappingService;
    @Autowired
    SupplierService supplierService;
    /**
     * 编制
     */
    @RequestMapping("/BianZhi")
    public boolean BianZhi(Contract contract){
        contractService.Insert(contract);
        return true;
    }

    @RequestMapping("/find")
    public Layui<Contract> find1(@Param("limit") Integer limit,/*页大小*/
                                 @Param("page") Integer page/*页码*/, Contract ord){
        ord.setStatus("未确定");
        int page1 = (page - 1) * limit;
        List<Contract> orders=contractService.findAll(ord,limit,page1);
        int count=contractService.getCount(ord);
        //新建layui分页需要的返回数据类型。
        Layui<Contract> lists = new Layui<Contract>();
        lists.setCode(0);
        lists.setCount(count);
        lists.setData(orders);
        return lists;
    }

    @RequestMapping("/find1")
    public Layui<Contract> find2(@Param("limit") Integer limit,/*页大小*/
                                 @Param("page") Integer page/*页码*/, Contract ord){
        ord.setStatus("确定");
        int page1 = (page - 1) * limit;
        List<Contract> orders=contractService.findAll(ord,limit,page1);
        int count=contractService.getCount(ord);
        //新建layui分页需要的返回数据类型。
        Layui<Contract> lists = new Layui<Contract>();
        lists.setCode(0);
        lists.setCount(count);
        lists.setData(orders);
        return lists;
    }

    /**
     * 确认
     */
    @RequestMapping("/QueRen")
    public boolean QueRen(Contract contract){
        contractService.Update(contract);
        return true;
    }

    /**
     * 合同明细
     */
    @RequestMapping("/Ming")
    public Contract Ming(Contract contract){
        Contract contract1 = contractService.SelectByCont(contract);
        return contract1;
    }

    /**
     * 根据合同id查orders
     */
    @RequestMapping("/OrderSel")
    public List<Orders> OrderSel(Contract contract){
        List<Orders> orders= ordersService.SelByContId(contract.getId());
        return orders;
    }

    /**
     * 根据id删除
     */
    @RequestMapping("/DelById")
    public boolean DelById(Contract contract){
        IdMapping idMapping=new IdMapping();
        idMapping.setContId(contract.getId());
        boolean b=idMappingService.UpdateById(idMapping);
        return contractService.DelById(contract);
    }

    /**
     * 合同查询
     */
    @RequestMapping("/selectContract")
    public Layui<Contract> selectContract(@Param("limit") Integer limit,/*页大小*/
                                          @Param("page") Integer page/*页码*/, Contract ord){
        Subject subject= SecurityUtils.getSubject();
        Employee employee =(Employee) subject.getSession().getAttribute("employee");//员工
        Supplier supplier = supplierService.selectSupplierById(employee.getId());//根据id查询供货商
        System.out.println(supplier);

        page= (page - 1) * limit;
        ord.setSeller(supplier.getAbbreviation());
        List<Contract> orders=contractService.SelectContract(ord,limit,page);
        int count=contractService.SelectContractCount(ord);
        //新建layui分页需要的返回数据类型。

        Layui<Contract> lists = new Layui<Contract>();
        lists.setCode(0);
        lists.setCount(count);
        lists.setData(orders);
        return lists;
    }

}
